<?php
class  Model_Tukhoa extends Zend_Db_Table
{
	protected $_db = 'null';
	protected $_tk ='tu_khoa_email';
	protected $_lh = 'lien_he';
	
	public function __construct()
	{
	  $this->_db =  Zend_Registry::get('connectDb');
	}
	// your code 
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : lấy alll từ khóa
	 */
	public function getalltukhoa()
	{
		$sql = $this->_db->select()
						 ->from($this->_tk)
						 ->where($this->_tk.'.su_dung = 1');
		return $this->_db->fetchAll($sql);				 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : lấy alll từ khóa và liên hệ 
	 */
	public function getalltukhoalienhe($idlh)
	{
		$sql = $this->_db->select()
						 ->from($this->_tk)
						 ->where($this->_tk.'.su_dung = 1');
		 $arrtukhoa = $this->_db->fetchAll($sql);
		 foreach ($arrtukhoa as $key => $value){
		 		$arrtukhoa[$key]["/".$value['ten_tu_khoa']."/"] =  $this->gettencuatukhoa($value['truong_lienhe'],$idlh);
		 		
		 		
		 }
		
		 return $arrtukhoa;				 
	}	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $truongcanlay
	 */
	public function gettencuatukhoa($uid,$truongcanlay,$emaillh)
	{
		$sql = $this->_db->select()
						 ->from($this->_lh,$truongcanlay)
						 ->where('email = "'.$emaillh.'"')
						 ->where('uid ='.$uid);
		
	return  $this->_db->fetchOne($sql);				 
						 
	}
	/**
	 * get tu khoa 
	 * Enter description here ...
	 */
	public function gettukhoa($uid,$idtukhoa,$emaillh){
	   
		$sql = $this->_db->select()
						 ->from($this->_tk)
						 ->where($this->_tk.'.su_dung = 1')
						 ->where('tk_id = ?',$idtukhoa);
		$query = $this->_db->fetchAll($sql);
		// lay kèm theo ten trường cần lây của từ khóa
		foreach ($query as $key => $value){
			 $query[$key]['lienhe'] =  $this->gettencuatukhoa($uid,$value['truong_lienhe'],$emaillh); 
		}
		
		//echo "<pre>"; print_r($query);
		//die();
							
	return $query;					 
			
	}
	// phần từ khóa
	public function getAlltukhoadanhsach()
	{
		$sql = $this->_db->select()
		                 ->from($this->_tk);
		 $query = $this->_db->fetchAll($sql);
	return $query;	                 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : change tu khoa su dung khong su dung
	 * @param unknown_type $data
	 */
	public function changetukhoasudung($data)
	{	
		$date = date('m-d-Y H:i:s');
		$mang = array(
				'su_dung' =>$data['thongso'],
				'ngay_sua' =>$date
		);
		$where = "tk_id =".$data['id'];
		$query = $this->_db->update($this->_tk, $mang,$where);
	return $query;
		
	}
	/**
	 * admin888<pavent@gmail.com>
	 * Enter description : them mới từ khóa
	 * @param unknown_type $data
	 */
	public function  addthemtukhoa($data)
	{
		$date = date('m-d-Y H:i:s');
		$mang = array(
				'uid'  				=>$data['idtao'], 
				'ten_tu_khoa'		=>"{".$data['tentukhoahienthi']."}", 
				'truong_lienhe'		=>$data['truongcanlay'], 
				'mo_ta'  	     	=>$data['mota'], 
				'ngay_tao'			=>$date,
				'ngay_sua'  		=>$date

		);
		$query = $this->_db->insert($this->_tk, $mang);
	return $query;	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description: get tu khoa theo id 
	 * @param unknown_type $idtukhoa
	 */
	public function  gettukhoatheoid($idtukhoa)
	{
		$sql = $this->_db->select()
						 ->from($this->_tk)
						 ->where($this->_tk.".tk_id = ".$idtukhoa);
		return $this->_db->fetchRow($sql);				 	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : update tukhoa
	 * @param unknown_type $data
	 */
	public function updatetukhoa($data)
	{
		$date = date('m-d-Y H:i:s');
		$mang = array(
			'truong_lienhe'		=>$data['truongcanlay'],
			'ten_tu_khoa'		=>"{".$data['tentukhoahienthi_edit']."}",
			'mo_ta'		        =>$data['mota_edit'],
			'ngay_sua'		    =>$date
			
		);
		$where = "tk_id = ".$data['idtukhoa'];
	return  $this->_db->update($this->_tk, $mang,$where);	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description xoa tu khoa 
	 * @param unknown_type $idtk
	 */
	public function  xoaonetukhoa($idtk)
	{
		$where = "tk_id = ".$idtk;
		$xoa =  $this->_db->delete($this->_tk,$where);
		return  $xoa;
	}
	/**
	 * admin888 <pavent@gmail.com>
	 * Enter description : mang lien hệ để tọa khoa 
	 */
	public function mangtukhoakienhe()
	{
		$arr = array(
				array(
					'truongtrondb' =>'tenlh',
					'hienthi'	=>'Tên liên hệ'
				),
				array(
					'truongtrondb' =>'email',
					'hienthi'	=>'Email liên hệ'
				),
				array(
					'truongtrondb' =>'dien_thoai',
					'hienthi'	=>'Điện thoại liên hệ'
				),
				array(
					'truongtrondb' =>'dia_chi',
					'hienthi'	=>'Địa chỉ liên hệ'
				),	
				array(
					'truongtrondb' =>'cong_ty',
					'hienthi'	=>'Tên công ty liên hệ'
				),
																					
	    );
	  return  $arr;  
	} 
	
}